<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<!--单条标记模态框-->
<div class="modal fade" id="modal-create">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">库存划项目标记</h4>
            </div>
            <div class="modal-body">
                <form id="addMarkForm" class="form-horizontal" role="form">
                    <input type="hidden" class="form-control" id="ipmBeforeItyId" name="ipmBeforeItyId">
                    <input type="hidden" id="matSpec">
                    <input type="hidden" id="matLength">
                    <input type="hidden" id="matWidth">
                    <input type="hidden" id="matHeight">
                    <input type="hidden" id="formulaType">
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">原料描述</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="materialDesc"
                                   readonly="readonly">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">计划物料描述</label>
                        <div class="col-sm-10">
                            <input type="text" autocomplete="off" class="form-control" name="ipmNameDescExcel"
                                   placeholder="请输入计划物料描述">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">新项目编号</label>
                        <div class="col-sm-10">
                            <input type="text" autocomplete="off" class="form-control" name="ipmInProjectCode"
                                   id="ipmInProjectCode" maxlength="8"
                                   placeholder="请输入8位新项目编号">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">新项目名称</label>
                        <div class="col-sm-10">
                            <input type="text" autocomplete="off" class="form-control" name="ipmInProjectName"
                                   id="ipmInProjectName"
                                   placeholder="请输入新项目名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">可用数量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="ityAvaNum" readonly="readonly">
                        </div>
                        <label class="col-sm-2 control-label no-padding-right">可用重量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="ityAvaWeight" readonly="readonly">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">标记数量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" name="ipmNum" id="ipmNum"
                                   autocomplete="off" placeholder="请输入标记数量" readonly="readonly">
                        </div>
                        <label class="col-sm-2 control-label no-padding-right">标记重量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" name="ipmWeight" id="ipmWeight"
                                   autocomplete="off" placeholder="请输入标记重量">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">备注</label>
                        <div class="col-sm-10">
                            <input type="text" autocomplete="off" class="form-control" name="ipmRemark"
                                   placeholder="请输入备注">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-addMark">确定</button>
            </div>
        </div>
    </div>
</div>

<section class="content-header">
    <h1>
        库存划项目标记操作
        <button onclick="javascript:window.history.back();" type="button" class="btn btn-success"
                style="text-align:center;vertical-align:middle;font-size:30px;position:absolute;right:3%;display: inline-block;background-color: transparent;border: none;color: #5c5c5c;outline:none;">
            <i style="-moz-transform:scaleX(-1);-webkit-transform:scaleX(-1);-o-transform:scaleX(-1);transform:scaleX(-1);filter:FlipH;"
               class="glyphicon glyphicon-share-alt"></i><span
                style="margin-left: 5px;font-size: 16px;position: absolute;top: 20%;">返回</span>
        </button>
    </h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>原料类型</label>
                                    <input type="text" name="matTypeName"
                                           class="form-control"
                                           placeholder="请输入原料类型">
                                </div>
                                <div class="form-group">
                                    <label>原料材质</label>
                                    <input type="text" name="matQualityName" class="form-control"
                                           placeholder="请输入原料材质">
                                </div>
                                <div class="form-group">
                                    <label>原料规格</label>
                                    <input type="text" name="matSpecName" class="form-control"
                                           placeholder="请输入原料规格">
                                </div>
                                <div class="form-group">
                                    <label>宽度</label>
                                    <input type="number" step="0.1" name="matWidth" class="form-control"
                                           placeholder="请输入原料宽度">
                                </div>
                                <div class="form-group">
                                    <label>长度</label>
                                    <input type="number" step="0.1" name="matLength" class="form-control"
                                           placeholder="请输入原料长度">
                                </div>
                                <div class="form-group">
                                    <label>米重/厚度</label>
                                    <input type="number" step="0.1" name="matHeight" class="form-control"
                                           placeholder="请输入原料米重/厚度">
                                </div>
                                <button type="button" class="btn btn-success" id="btn-selectStock">搜索</button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <table id="bootstrap-stock-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    /**加载表格*/
    var table;

    /** 打开单条标记模态框 */
    function mark(id, d) {
        $("#ipmBeforeItyId").val(id);
        $("#materialDesc").val(d.matNameDesc);

        $("#ityAvaNum").val(d.ityAvaNum);
        $("#ityAvaWeight").val(d.ityAvaWeight);

        $("#matSpec").val(d.matSpecName);
        $("#matLength").val(d.matLength);
        $("#matWidth").val(d.matWidth);
        $("#matHeight").val(d.matHeight);
        $("#formulaType").val(d.matFormulaType);

        js.modal.open("modal-create");
    }

    $(function () {

        table = js.table.init({
            id: "bootstrap-stock-table",
            url: ctx + "matInvManage/complex/list",
            showExport: false,
            queryData: {ityInType: 1, ityAvaWeightZeroFlg: 2},
            columns: [
                {
                    title: '序号', field: '', width: '40',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: '类型', field: 'matTypeName'},
                {title: '材质', field: 'matQualityName'},
                {title: '规格', field: 'matSpecName'},
                {title: '宽度', field: 'matWidth'},
                {title: '长度', field: 'matLength'},
                {title: '米重/厚度', field: 'matHeight'},
                {title: '特殊说明', field: 'itySpec'},
                {title: '可用数量', field: 'ityAvaNum'},
                {title: '可用重量', field: 'ityAvaWeight'},
                {title: '库位', field: 'whiName'},
                {
                    title: '操作', width: '60',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push("<a class='btn btn-info btn-xs'  href='javascript:void(0);' onclick='mark(" + row.ityId + "," + JSON.stringify(row) + ")'>标记</a>");
                        return actions.join('');
                    }
                }
            ],
            rowStyle: function (row, index) {
                if (row.itySpec != null && row.itySpec.indexOf("不合格") >= 0) {
                    return {css: {"color": "red"}}
                } else {
                    return {css: {"color": "black"}}
                }
            }
        });

        /** 表格搜索库存在库 */
        $("#btn-selectStock").click(function () {
            js.table.search(table);
        });

        /**标记数量变更 联动标记重量（20251128号原料梁元反馈类型不同，需要通过重量核算数量进行修改）*/
        // $("#ipmNum").change(function () {
        //
        //     var spec = $("#matSpec").val();
        //     var length = $("#matLength").val();
        //     var width = $("#matWidth").val();
        //     var height = $("#matHeight").val();
        //     var needQty = $("#ipmNum").val();
        //     var formulaType = $("#formulaType").val();
        //
        //     var totalWeight = returnMaterialWeight(spec, length, width, height, needQty, formulaType);
        //     $("#ipmWeight").val(totalWeight);
        // });

        /**标记重量核算数量（新增）**/
        $("#ipmWeight").change(function () {
            var spec = $("#matSpec").val();
            var length = $("#matLength").val();
            var width = $("#matWidth").val();
            var height = $("#matHeight").val();
            var needQty = $("#ipmWeight").val();
            var formulaType = $("#formulaType").val();

            var totalNum = returnMaterialNum(spec, length, width, height, needQty, formulaType);
            $("#ipmNum").val(totalNum);
        });

        /**关闭单条标记模态框*/
        js.modal.hideEvent("modal-create", function () {
            js.reset("addMarkForm");
            js.validate.reset("addMarkForm");
        });

        /**保存单条库存划项目标记*/
        $("#btn-addMark").click(function () {

            var ipmInProjectCode = $("#ipmInProjectCode").val();
            js.post({
                url: ctx + "matInvManage/complex/checkProCode",
                data: {proCode: $("#ipmInProjectCode").val()},
                success: function (result) {
                    if (result.type === 'SUCCESS') {
                        var ipmWeight = $("#ipmWeight").val();
                        if (ipmWeight == null || ipmWeight == "") {
                            js.modal.warning("请输入标记重量！");
                            $("#ipmWeight").focus();
                            return false;
                        }

                        var ipmNum = $("#ipmNum").val();
                        var ityAvaNum = $("#ityAvaNum").val();
                        var ityAvaWeight = $("#ityAvaWeight").val();

                        if (ipmNum != null && ipmNum != "" && ityAvaNum != null && ityAvaNum != "" && parseFloat(ipmNum) > parseFloat(ityAvaNum)) {

                            js.modal.warning("标记数量大于可用数量！");
                            $("#ipmNum").focus();
                            return false;
                        }

                        if (ipmWeight != null && ipmWeight != "" && ityAvaWeight != null && ityAvaWeight != "" && parseFloat(ipmWeight) > parseFloat(ityAvaWeight)) {

                            js.modal.warning("标记重量大于可用重量！");
                            $("#ipmWeight").focus();
                            return false;
                        }

                        js.validSubmit({
                            formId: "addMarkForm",
                            url: ctx + "material/ityToPro/save",
                            data: new FormData($("#addMarkForm")[0]),
                            success: function (result) {
                                if (result.type === web_status.SUCCESS) {
                                    js.modal.hide("modal-create");
                                    js.modal.success("操作成功");
                                    js.table.refresh(table);
                                } else {
                                    js.modal.warning(result.msg);
                                }
                            }
                        });
                    } else {
                        js.modal.warning("项目号不存在！");
                        $("#ipmInProjectCode").focus();
                        return;
                    }
                }
            });
        })

        /** 单条标记验证 */
        js.validate.init("addMarkForm", {
            fields: {
                // ipmNameDescExcel: {validators: {notEmpty: {message: '请输入计划物料描述'}}},
                ipmInProjectCode: {
                    validators: {
                        threshold: 6, //有6字符以上才发送ajax请求，（input中输入一个字符，插件会向服务器发送一次，设置限制，6字符以上才开始）
                        notEmpty: {
                            message: '请输入新项目编号'
                        },
                        // 发送ajax请求到后台查询，返回 valid：true或者false
                        callback: {
                            message: "项目号不存在",
                            callback: function (value, validator) {
                                var flag = false;
                                $.ajax({
                                    url: ctx + 'material/changeOrder/codeExist', // 请求路径
                                    type: 'POST',
                                    async: false,	// 同步请求，必须是false
                                    data: {
                                        ipmInProjectCode: $('#ipmInProjectCode').val() // 请求参数
                                    },
                                    success: function (res) {
                                        flag = $.parseJSON(res).valid;
                                    }
                                });
                                return flag;
                            }
                        }
                    }
                },
                ipmInProjectName: {validators: {notEmpty: {message: '请输入新项目名称'}}},
            }
        });

        /** 项目编号 焦点离开 自动取得 名称 */
        $("#ipmInProjectCode").blur(function () {
            js.get({
                url: ctx + "material/changeOrder/findProject",
                data: {proCode: $("#ipmInProjectCode").val()},
                success: function (result) {
                    if (result.data == null) {
                        $("#ipmInProjectName").val("");
                    } else {
                        $("#ipmInProjectName").val(result.data.proName);
                    }
                }
            });
        });

    })

</script>
</body>
</html>